﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using IRUM.Core;
using IRUM.XingAPI;
using IRUM.Utils;

namespace IRUM.TR
{
    /// <summary>
    /// 미결제약정
    /// </summary>
    public class OpenYak
    {
        public string shcode = string.Empty;    //종목코드
        public string gubun = string.Empty;     //일분구분, D:일, M:분
        public string date = string.Empty;      //"yyyy/mm/dd"
        public string time = string.Empty;      //"hhmmss
        public long openopenyak = 0;
        public long highopenyak = 0;
        public long lowopenyak = 0;
        public long closeopenyak = 0;
        public long openupdn = 0;
    }

    public class OpenYakArgs : EventArgs
    {
        public List<OpenYak> RetData = null;

        public OpenYakArgs(List<OpenYak> data)
        {
            RetData = data;
        }
    }

    public delegate void OpenYakHandler(object s, OpenYakArgs e);
    /// <summary>
    /// 야간선물 미결제약정
    /// </summary>
    public class t8409 : CBaseQuery
    {
        public event OpenYakHandler OnReceived;
        private List<OpenYak> m_list = new List<OpenYak>();

        private string focode = string.Empty;
        private string bdgubun = string.Empty;

        /// <summary>
        /// 야간선물 미결제약정
        /// </summary>
        /// <param name="focode">선물옵션코드, 8</param>
        public t8409(string focode, string bdgubun)
        {
            this.focode = focode;
            this.bdgubun = bdgubun;

            SetClassData(TR_Type.t8409);
        }

        public void SetOutBlockData(int Index)
        {
            OpenYak cd = new OpenYak();

            cd.shcode = focode;
            cd.gubun = bdgubun;

            if (bdgubun.Equals("M") == true)
            {
                cd.date = IRUM_Utils.GetLastTradingDateEvening(DateTime.Now).ToString("yyyyMMdd");    //마지막 거래일 가져와서 처리해야 함.
                cd.time = m_Query.GetFieldData(m_OutBlock1Name, "dt", Index);
            }
            else
            {
                cd.date = m_Query.GetFieldData(m_OutBlock1Name, "dt", Index);
                cd.time = "290000";
            }
            cd.openopenyak = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "openopenyak", Index));
            cd.highopenyak = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "highopenyak", Index));
            cd.lowopenyak = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "lowopenyak", Index));
            cd.closeopenyak = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "closeopenyak", Index));
            cd.openupdn = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "openupdn", Index));

            m_list.Add(cd);
        }

        public override void HandleQuery()
        {
            int OccursCount = m_Query.GetBlockCount(m_OutBlock1Name);

            for (int OccursIndex = 0; OccursIndex < OccursCount; OccursIndex++)
            {
                try
                {
                    SetOutBlockData(OccursIndex);
                }
                catch (Exception e)
                {
                    Debug.Print(e.ToString());
                    continue;
                }
            }

            if (OnReceived != null)
            {
                OnReceived(this, new OpenYakArgs(m_list));
            }
        }

        protected override void SetRequestData()
        {
            m_Query.SetFieldData(m_InBlockName, "focode", 0, focode);  //선물옵션코드, 8
            m_Query.SetFieldData(m_InBlockName, "bdgubun", 0, bdgubun.Equals("M")?"1":"2");  //분일구분, 1, 0:30초, 1:분, 2:일
            m_Query.SetFieldData(m_InBlockName, "nmin", 0, "1");  //분간격, 3, n분
            m_Query.SetFieldData(m_InBlockName, "tcgubun", 0, "1");  //당일연결구분, 1, 0:전체, 1:당일
            m_Query.SetFieldData(m_InBlockName, "cnt", 0, bdgubun.Equals("M") ? "1000" : "500");    //조회건수, 4
        }
    }
}
